#include "studentdialog.h"
#include "ui_studentdialog.h"

#include <QMessageBox>

StudentDialog::StudentDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::StudentDialog)
{
    ui->setupUi(this);
    createDB();
    createTable();
    queryTable();

    QObject::connect(ui->idEdit, SIGNAL(textChanged(QString)), this, SLOT(enableButton()));
    QObject::connect(ui->nameEdit, SIGNAL(textChanged(QString)), this, SLOT(enableButton()));
    QObject::connect(ui->ScoreEdit, SIGNAL(textChanged(QString)), this, SLOT(enableButton()));
}

StudentDialog::~StudentDialog()
{
    delete ui;
}

void StudentDialog::createDB()
{
    // 添加数据库驱动
    db = QSqlDatabase::addDatabase("QSQLITE");

    // 设置数据库名字(文件名)
    db.setDatabaseName("student.db");

    // 打开数据库
    if(true == db.open())
    {
        qDebug() << "创建/打开数据库成功";
    }
    else
    {
        qDebug() << "创建/打开数据库失败";
    }
}

void StudentDialog::createTable()
{
    QSqlQuery query;    // 注意这里必须要在打开数据库的情况下，才能无参构造该对象，这里是已经创建/打开数据库了，所以才能无参创建
    // 这里每次都以一对双引号结束，这样会自动拼接成一个完整的字符串
    QString str = QString("create table student ("
                          "id int primary key not null,"
                          "name text not null,"
                          "score real not null)");
    if(query.exec(str))
        qDebug() << "创建表成功";
    else
        qDebug() << "失败：" << str;
}

void StudentDialog::queryTable()
{
    QString str = QString("select * from student");
    model.setQuery(str);
    ui->tableView->setModel(&model);
}

void StudentDialog::on_insertButton_clicked()
{
    QSqlQuery query;
    int id = ui->idEdit->text().toInt();
    QString name = ui->nameEdit->text();
    double score = ui->ScoreEdit->text().toDouble();

    QString str = QString("insert into student values (%1,'%2', %3)").arg(id).arg(name).arg(score);
    if(query.exec(str))
    {
        qDebug() << "插入数据成功!";
        queryTable();
    }
    else
    {
        qDebug() << "插入数据失败!: " << str;
    }
}


void StudentDialog::on_deleteButton_clicked()
{
    QSqlQuery query;
    int id = ui->idEdit->text().toInt();

    QString str = QString("delete from student where id=%1").arg(id);
    if(QMessageBox::question(this, "Tips", "确定删除么?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
        return;
    if(query.exec(str))
    {
        qDebug() << "删除数据成功!: " << str;
        queryTable();
    }
    else
    {
        qDebug() << "删除数据失败!: " << str;
    }
}


void StudentDialog::on_updateButton_clicked()
{
    QSqlQuery query;
    int id = ui->idEdit->text().toInt();
    double score = ui->ScoreEdit->text().toDouble();

    QString str = QString("update student set score=%1 where id=%2").arg(score).arg(id);
    if(QMessageBox::question(this, "Tips", "确定修改么?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
        return;
    if(query.exec(str))
    {
        qDebug() << "修改数据成功!: " << str;
        queryTable();
    }
    else
    {
        qDebug() << "修改数据失败!: " << str;
    }
}


void StudentDialog::on_sortButton_clicked()
{
    QSqlQuery query;
    QString value = ui->valueComboBox->currentText();
    QString cond = ui->condComboBox->currentText() == "升序" ? "asc" : "desc";
    QString str = QString("select * from student order by %1 %2").arg(value).arg(cond);
    if(query.exec(str))
    {
        qDebug() << "排序数据成功!: " << str;
        QString sql = str;
        model.setQuery(sql);
        ui->tableView->setModel(&model);
    }
    else
    {
        qDebug() << "排序数据失败!: " << str;
    }
}

void StudentDialog::enableButton()
{
    ui->insertButton->setEnabled(!ui->idEdit->text().isEmpty() && !ui->nameEdit->text().isEmpty() && !ui->ScoreEdit->text().isEmpty());
    ui->deleteButton->setEnabled(!ui->idEdit->text().isEmpty());
    ui->updateButton->setEnabled(!ui->idEdit->text().isEmpty() && !ui->ScoreEdit->text().isEmpty());
    // if(!ui->idEdit->text().isEmpty() && !ui->nameEdit->text().isEmpty() && !ui->ScoreEdit->text().isEmpty())
    //     ui->insertButton->setEnabled(true);
    // else
    //     ui->insertButton->setEnabled(false);
}

